<template>
    <section
            v-loading.fullscreen="importLoading"
            element-loading-text="拼命提交中"
            element-loading-spinner="el-icon-loading"
    >
        <br><br>
        <el-form inline class="demo-ruleForm" label-width="280px" :model="conItem" :rules="conFormRules" ref="conItem">
            <el-form-item label="合同类型:" prop="type">
                <!--<el-radio v-model="conItem.type" v-for="(txt, value) in dict.contract_type" :label="value" :key="value">{{ txt }}</el-radio>-->
                <el-select v-model="conItem.type" @change="selectTypeChange">
                    <el-option value="">请选择合同类型</el-option>
                    <el-option v-for="(txt, value) in dict.contract_type" :key="value" :label="txt" :value="value"></el-option>
                </el-select>
            </el-form-item>
            <div>
                <br>
                <el-form-item label="客户类型:" prop="customer_type" v-if="conItem.type == 4 && conItem.id == ''">
                    <el-radio-group v-model="conItem.customer_type"  @change="radioCustomerTypeChange">
                        <el-radio v-for="(txt, value) in dict.contract_customer_type" :label="value" :key="value">{{ txt }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <br><br><el-col>基本信息</el-col><br><br>
                <div v-if="conItem.customer_type == 1">
                    <el-form-item label="客户姓名:" prop="customer_full_name">
                        <el-autocomplete :trigger-on-focus="false" v-model="conItem.customer_full_name" :fetch-suggestions="wtCodeQuerySearchAsync"
                                         @select="handleWtCodeSelect"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="证件类型:" prop="customer_identity_type">
                        <el-select v-model="conItem.customer_identity_type">
                            <el-option v-for="(txt, value) in dict.contract_identity_type" :key="value" :label="txt" :value="value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="证件号码:" prop="customer_identity_no"><el-input v-model="conItem.customer_identity_no"></el-input></el-form-item>

                    <el-form-item label="性别:" prop="customer_gender">
                        <el-select v-model="conItem.customer_gender">
                            <el-option v-for="(txt, value) in dict.contract_customer_gender" :key="value" :label="txt" :value="value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="联系电话:" prop="customer_mobile"><el-input v-model="conItem.customer_mobile"></el-input></el-form-item>
                    <el-form-item label="联系地址:" prop="customer_address"><el-input v-model="conItem.customer_address"></el-input></el-form-item>
                </div>
                <div v-else>
                    <el-form-item label="企业名称:" prop="company_name">
                        <el-autocomplete :trigger-on-focus="false" v-model="conItem.company_name" :fetch-suggestions="wtCodeQuerySearchAsync"
                                         @select="handleWtCodeSelect"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="营业执照号码/统一社会信用代码:" prop="credit_no"><el-input v-model="conItem.credit_no"></el-input></el-form-item>
                    <el-form-item label="组织机构代码证号码:" prop="customer_identity_no"><el-input v-model="conItem.customer_identity_no"></el-input></el-form-item>
                    <el-form-item label="法定代表人或负责人:" prop="customer_full_name"><el-input v-model="conItem.customer_full_name"></el-input></el-form-item>
                    <el-form-item label="联系电话:" prop="customer_mobile">
                        <el-input v-model="conItem.customer_mobile"
                                  :conFormRules="[{ required: true, message: '请输入联系电话', trigger: 'blur' }]"
                        ></el-input>
                    </el-form-item>
                </div>
                <br><br><el-col>销售信息</el-col><br><br>
                <el-form-item label="销售姓名:" prop="sale_name">
                    <el-autocomplete :trigger-on-focus="false" v-model="conItem.sale_name_split" :fetch-suggestions="querySearchAsyncSale"
                                     @select="handleSelectSale"></el-autocomplete>
                </el-form-item>


                <br><br> <el-col>合同信息</el-col><br><br>
                <!-- ============================================ 私募表单 start ================================================ -->
                <div v-if="conItem.type == 4">
                    <el-form-item label="合同编号:" prop="no"><el-input v-model="conItem.no"></el-input></el-form-item>
                    <el-form-item label="产品名称:" prop="product_info">
                        <el-select v-model="conItem.product_info" @change="selectChange">
                            <el-option :key="item.value" :label="item.key" :value="item" v-for="item in dynamicProducts" ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="合同金额:" prop="amount"><el-input v-model="conItem.amount"></el-input></el-form-item>
                    <el-form-item label="合同生效日:" prop="term_start">
                        <div class="block">
                            <el-date-picker v-model="conItem.term_start" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" size="large"
                                            :clearable='false' :editable="false" default-value :picker-options="pickerOptionsBeforeNow"></el-date-picker>
                        </div>
                    </el-form-item>
                </div>
                <!-- ============================================ 私募表单 end ================================================ -->

                <!-- ============================================ 有限合伙等表单 start ================================================ -->
                <div v-else>
                    <el-form-item label="合同编号:" prop="no"><el-input v-model="conItem.no"></el-input></el-form-item>
                    <el-form-item label="产品名称:" prop="product_info">
                        <el-select v-model="conItem.product_info" @change="selectChange2">
                            <el-option :key="item.value" :label="item.key" :value="item" v-for="item in dynamicProducts2" ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="投资期限:" prop="investment_period">
                        <!--<el-input v-model="conItem.investment_period" disabled></el-input>-->
                        <el-select v-model="conItem.investment_period" disabled>
                            <el-option v-for="(txt, value) in dict.contract_investment_period" :key="value" :label="txt" :value="value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="客户收益返还方式:" prop="income_return_type">
                        <!--<el-input v-model="conItem.income_return_type" disabled :formatter="returnTypeFormatter"></el-input>-->
                        <el-select v-model="conItem.income_return_type" disabled>
                            <el-option v-for="(txt, value) in dict.contract_income_return_type" :key="value" :label="txt" :value="value"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="预期收益率(%):" prop="expected_rate_of_return">
                        <el-input v-model="conItem.expected_rate_of_return" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="合同金额:" prop="amount"><el-input v-model="conItem.amount"></el-input></el-form-item>
                    <el-form-item label="预期收益(元):" prop="expected_earnings"><el-input v-model="conItem.expected_earnings"></el-input></el-form-item>
                    <el-form-item label="月返还收益(元):" prop="month_return"><el-input v-model="conItem.month_return"></el-input></el-form-item>
                    <el-form-item label="合同生效日:" prop="term_start">
                        <div class="block">
                            <el-date-picker v-model="conItem.term_start" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" size="large"
                                            :clearable='false' :editable="false" default-value :picker-options="pickerOptionsBeforeNow"></el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item label="合同到期日:" prop="term_end">
                        <div class="block">
                            <el-date-picker v-model="conItem.term_end" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" size="large"
                                            :clearable='false' :editable="false" default-value :picker-options="pickerOptionsBeforeNow"></el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item label="赠金:" prop="donation_amount"><el-input v-model="conItem.donation_amount"></el-input></el-form-item>
                    <el-form-item label="到账方式:" prop="payment_method"><el-input v-model="conItem.payment_method"></el-input></el-form-item>
                    <el-form-item label="签订类型:" prop="sign_type">
                        <el-select v-model="conItem.sign_type">
                            <el-option v-for="(txt, value) in dict.contract_sign_type" :key="value" :label="txt" :value="value"></el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <!-- ============================================ 有限合伙等表单 end ================================================ -->



                <br><br><el-col>银行信息</el-col><br><br>
                <el-form-item label="账户名称:" prop="bank_account_name"><el-input v-model="conItem.bank_account_name"></el-input></el-form-item>
                <el-form-item label="银行账号:" prop="bank_no"><el-input v-model="conItem.bank_no"></el-input></el-form-item>
                <el-form-item label="开户行名称:" prop="bank_branch_name"><el-input v-model="conItem.bank_branch_name"></el-input></el-form-item>
                <el-form-item label="开户行地址:" prop="bank_address"><el-input v-model="conItem.bank_address"></el-input></el-form-item>
                <br><br>
                <el-col>附件信息</el-col><br><br>
                <el-form-item label="文件上传:">
                    <el-upload class="upload-demo" action="ss" :before-upload="beforeAvatarUpload" :http-request="httpRequest" :file-list="conItem.attachment_info"
                               :limit="upload_file_limit" :on-exceed="handleExceed" :on-remove="handleRemove"><!--:show-file-list="false"-->
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <br><br>
                <!--<div v-if="conItem.id">-->
                <!--<el-col>合同状态</el-col><br><br>-->
                <!--<el-form-item label="合同状态:" prop="state"><el-input v-model="conItem.state"></el-input></el-form-item>-->
                <!--<el-form-item label="备注:" prop="remark"><el-input v-model="conItem.remark"></el-input></el-form-item>-->
                <!--</div>-->
                <el-row :gutter="20">
                    <el-col :span="12" :offset="6">
                        <el-form-item>
                            <el-button v-if="conItem.id" type="primary" @click="saveItem('conItem')" size="small">保 存</el-button>
                            <el-button v-else type="primary" @click="showItem('conItem')" size="small">预 览</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="backButton" size="small">取 消</el-button>
                        </el-form-item>

                    </el-col>
                </el-row>
            </div>


        </el-form>

        <el-dialog title="预览" :visible.sync="dialogVisible" width="46%">
            <el-form v-model="conDialogItem" inline label-width="280px">
                <br><br><el-col>基本信息</el-col><br><br>
                <div v-if="conDialogItem.customer_type == 1">
                    <el-form-item label="客户姓名:"><span>{{conDialogItem.customer_full_name}} </span></el-form-item>
                    <el-form-item label="证件类型:"><span>{{dict.contract_identity_type[conDialogItem.customer_identity_type]}} </span></el-form-item>
                    <el-form-item label="性别:"><span>{{ dict.contract_customer_gender[conDialogItem.customer_gender] }} </span></el-form-item>
                    <el-form-item label="证件号码:"><span>{{ conDialogItem.customer_identity_no }} </span></el-form-item>
                    <el-form-item label="联系电话:"><span>{{ conDialogItem.customer_mobile }} </span></el-form-item>
                    <el-form-item label="联系地址:"><span>{{ conDialogItem.customer_address }} </span></el-form-item>
                </div>
                <div v-else>
                    <el-form-item label="企业名称:"><span>{{conDialogItem.company_name}} </span></el-form-item>
                    <el-form-item label="营业执照号码/统一社会信用代码:"><span>{{conDialogItem.credit_no}} </span></el-form-item>
                    <el-form-item label="组织机构代码证号码:"><span>{{ conDialogItem.customer_identity_no }} </span></el-form-item>
                    <el-form-item label="法定代表人或负责人:"><span>{{ conDialogItem.customer_full_name }} </span></el-form-item>
                    <el-form-item label="联系电话:"><span>{{conDialogItem.customer_mobile}} </span></el-form-item>
                </div>

                <br><br><el-col>销售信息</el-col><br><br>
                <el-form-item label="销售姓名:"><span>{{conDialogItem.sale_name_split}}</span></el-form-item>


                <br><br> <el-col>合同信息</el-col><br><br>
                <!-- ============================================ 私募表单 end ================================================ -->
                <div v-if="conItem.type == 4">
                    <el-form-item label="合同编号:"><span>{{conDialogItem.no}}</span></el-form-item>
                    <el-form-item label="产品名称:"><span>{{conDialogItem.product_name}} </span></el-form-item>
                    <el-form-item label="合同金额:"><span>{{conDialogItem.amount}} </span></el-form-item>
                    <el-form-item label="合同生效日:"><span>{{conDialogItem.term_start}} </span></el-form-item>
                </div>

                <!-- ============================================ 私募表单 end ================================================ -->

                <!-- ============================================ 有限合伙等表单 start ================================================ -->
                <div v-else>
                    <el-form-item label="合同编号:"><span>{{conDialogItem.no}}</span></el-form-item>
                    <el-form-item label="产品名称:"><span>{{conDialogItem.product_name}}</span></el-form-item>
                    <el-form-item label="投资期限:"><span>{{dict.contract_investment_period[conDialogItem.investment_period]}}</span></el-form-item>
                    <el-form-item label="客户收益返还方式:"><span>{{dict.contract_income_return_type[conDialogItem.income_return_type]}}</span></el-form-item>

                    <el-form-item label="预期收益率(%):"><span>{{conDialogItem.expected_rate_of_return}}</span></el-form-item>
                    <el-form-item label="合同金额:"><span>{{conDialogItem.amount}}</span></el-form-item>
                    <el-form-item label="预期收益(元):"><span>{{conDialogItem.expected_earnings}}</span></el-form-item>
                    <el-form-item label="月返还收益(元):"><span>{{conDialogItem.month_return}}</span></el-form-item>
                    <el-form-item label="合同生效日:"><span>{{conDialogItem.term_start}}</span></el-form-item>
                    <el-form-item label="合同到期日:"><span>{{conDialogItem.term_end}}</span></el-form-item>
                    <el-form-item label="赠金:"><span>{{conDialogItem.donation_amount}}</span></el-form-item>
                    <el-form-item label="到账方式:"><span>{{conDialogItem.payment_method}}</span></el-form-item>
                    <el-form-item label="签订类型:"><span>{{dict.contract_sign_type[conDialogItem.sign_type]}}</span> </el-form-item>
                </div>
                <!-- ============================================ 有限合伙等表单 end ================================================ -->


                <br><br>
                <el-col>银行信息</el-col><br><br>
                <el-form-item label="账户名称:"><span>{{conDialogItem.bank_account_name}}</span></el-form-item>
                <el-form-item label="银行账号:"><span>{{conDialogItem.bank_no}}</span></el-form-item>
                <el-form-item label="开户行名称:"><span>{{conDialogItem.bank_branch_name}}</span></el-form-item>
                <el-form-item label="开户行地址:"><span>{{conDialogItem.bank_address}}</span></el-form-item>
                <br><br>
                <el-col>附件信息</el-col><br><br>
                <el-form-item label="">
                    <p v-for="(key) in upload_file_names"><span>{{key.temp_file_name}}</span></p>
                </el-form-item>
                <br><br>
                <el-form-item label="">
                    <el-button type="primary" @click="addSaveItem" size="small">提 交</el-button>
                    <el-button type="primary" @click="dialogVisible = false" size="small">取 消</el-button>
                </el-form-item>


            </el-form>

        </el-dialog>



    </section>
</template>
<script>
    import * as API from '../../api/contract_api';
    import * as sale_api from '../../api/sale_api';
    import * as product_api from '../../api/product_api';
    import * as customer_api from '../../api/customer_api';
    import {update_file} from '../../api/common';
    import * as commons from '../../common/js/commons';
    import dict from '../../common/js/dict';
    import * as checkFields from '../../common/js/checkFields';

    export default {
        data () {
            return {
                process_status:false,
                importLoading: false,
                pickerOptionsBeforeNow: {
                    disabledDate(time) {
                        // return time.getTime() > Date.now() - 8.64e6      //此处为今天以前的
                    }
                },
                conItem: {
                    id: '',
                    type: '1',                    //合同类型 默认有限合伙
                    customer_type: '1',           //客户类型 默认个人
                    sign_type: '1',                   //签订类型，默认 新签
                    attachment_id_list: [],        //上传保存id数组
                    attachment_info: []             //回显上传
                },
                tempConItem: {},
                temp_attachment_id_list: [],    //暂存上传保存id数组
                dynamicProducts: [],            //动态获取产品名称的列表数据
                dynamicProducts2: [],           //动态获取产品名称的列表数据 -- 非私募
                upload_file_limit: 20,
                upload_file_names: [],    //上传文件的名字

                conDialogItem: {},
                dict: dict,
                conFormRules:{
                    type: {required: true,message:"请选择合同类型!", trigger: 'change'},
                    customer_type: {required: true,message:"请选择客户类型!", trigger: 'change'},
                    customer_full_name: [{ required: true,message:"请输入客户姓名!", trigger: 'blur'}],
                    company_name: [{ required: true,message:"请输入企业名称!", trigger: 'blur'}],
                    credit_no: [{ required: true,message:"请输入营业执照号码/统一社会信用代码!", trigger: 'blur'}],
                    customer_identity_type: [
                        { required: true,message:"请选择证件类型!",trigger: 'change'}
                    ],
                    customer_identity_no:[
                        { required: true, message:"请输入证件号码!", trigger: 'blur'},
                        { required: true, message:"请输入证件号码!", trigger: 'change'},
                    ],
                    customer_gender:[
                        {required: true, message:"请选择性别!",trigger: 'change'}
                    ],
                    sale_name:[
                        // {required: true, message:"请选择下列销售姓名!",trigger: 'blur'},
                        {required: true, message:"请选择下列销售姓名!",trigger: 'change'}
                    ],
                    no:[
                        {required: true, message:"请输入合同编号!", trigger: 'blur'}
                    ],
                    product_info:[
                        {required: true, message:"请选择产品名称!", trigger: 'change'}
                    ],
                    amount:[
                        {required: true, message:"请输入合同金额!", trigger: 'blur'}
                    ],
                    expected_earnings:[
                        {required: true, message:"请输入预期收益(元)!", trigger: 'blur'}
                    ],
                    sign_type:[
                        {required: true, message:"请输入签订类型!", trigger: 'change'}
                    ],
                    term_start:[
                        {required: true, message:"请选择合同生效日!", trigger: 'change'}
                    ],
                    term_end:[
                        {required: true, message:"请选择合同到期日!", trigger: 'change'}
                    ],
                    bank_account_name:[
                        {required: true, message:"请输入账户名称!", trigger: 'blur'}
                    ],
                    bank_no:[
                        {required: true, message:"请输入银行账号!", trigger: 'blur'}
                    ],
                    bank_branch_name:[
                        {required: true, message:"请输入开户行名称!", trigger: 'blur'}
                    ],
                    bank_address:[
                        {required: true, message:"请输入开户行地址!", trigger: 'blur'}
                    ]

                },

                dialogVisible: false
            }
        },created () {
            // 组件创建完后获取数据，
            // 此时 data 已经被 observed 了
            this.fetchData()
        },
        methods: {
            fetchData(){
                this.conItem.id = this.$route.params.id;
            },
            isBirthDate(val){
                return commons.isBirthDate(val);
            },
            investmentFormatter(row, column) {
                return dict.contract_investment_period['' + row[column.property]];
            },
            returnTypeFormatter(row, column) {
                return dict.contract_income_return_type['' + row[column.property]];
            },
            radioCustomerTypeChange() {
                this.conItem.product_info = '';
                this.conItem = {
                    id: '',
                    type: this.conItem.type,      //合同类型 默认有限合伙
                    customer_type: this.conItem.customer_type,           //客户类型 默认个人
                    sign_type: '1',               //签订类型，默认 新签
                    attachment_id_list: [],       //上传保存id数组
                };
            },
            selectTypeChange(val) {
                this.getProducts(val);
                if(val != 4){
                    this.conItem.customer_type = 1;
                }
                this.radioCustomerTypeChange();
            },
            selectChange(item) {
                this.conItem.product_info = item.key;
                this.conItem.product_name = item.key;
                this.conItem.product_id = item.value;
            },
            selectChange2(item) {
                this.conItem.product_info = item.key;
                var key = item.key.split('-');
                this.conItem.product_name = key[0];
                this.conItem.product_id = item.value;
                this.conItem.investment_period = item.investment_period + '';
                this.conItem.income_return_type = item.income_return_type + '';
                this.conItem.expected_rate_of_return = item.expected_earnings/10000;

            },
            getItem(){

                API.contractInfo({"id":this.conItem.id}).then((res)=>{
                    let result = JSON.parse(res.data.datas);
                    if(res.data.status == 200){
                        this.conItem = result;
                        this.conItem.attachment_id_list = [];

                        this.conItem.customer_identity_type = this.conItem.customer_identity_type + '';
                        this.conItem.customer_gender = this.conItem.customer_gender + '';
                        this.conItem.type = this.conItem.type + '';
                        this.conItem.customer_type = this.conItem.customer_type + '';
                        this.conItem.product_info = this.conItem.product_name;
                        var department_name = this.conItem.sale_department_name==null ? '' : '/' + this.conItem.sale_department_name;
                        var team_name = this.conItem.sale_team_name==null ? '' : '/' + this.conItem.sale_team_name;
                        var sale_position_name = this.conItem.sale_position_name==null ? '' : '/' + this.conItem.sale_position_name;
                        this.conItem.sale_name_split = this.conItem.sale_name + department_name + team_name + sale_position_name;

                        // <!-- ============================================ 有限合伙等表单 start ================================================ -->
                        var name = this.conItem.product_name == null ? '' : this.conItem.product_name;
                        var p = this.conItem.investment_period == null ? '' : " - " + dict.contract_investment_period[this.conItem.investment_period];    //投资期限
                        var t = this.conItem.income_return_type == null ? '' : " - " + dict.contract_income_return_type[this.conItem.income_return_type];    //客户收益返还方式
                        var r = this.conItem.expected_earnings == null ? '' : " - " + (this.conItem.expected_earnings/10000); //预期收益率
                        this.conItem.product_info = name + p + r + t;
                        this.conItem.investment_period = this.conItem.investment_period + '';
                        this.conItem.income_return_type = this.conItem.income_return_type + '';


                        // -------------------------------------------  设计金额和率的分别除以100和10000 ---------------------------------------------
                        this.conItem.expected_rate_of_return = this.conItem.expected_rate_of_return/10000;
                        this.conItem.amount = this.conItem.amount/100;
                        this.conItem.expected_earnings = this.conItem.expected_earnings/100;
                        this.conItem.month_return = this.conItem.month_return/100;
                        this.conItem.sign_type = this.conItem.sign_type + '';

                        this.attachmentInfoInit();
                    }else{
                        if(res.data.status === 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.status+" "+res.data.msg);
                    }
                });
            },

            /* ================= 设置上传文件ids组合在一起 ============================== */
            attachmentInfoInit() {
                if (this.conItem.attachment_info && this.conItem.attachment_info.length > 0) {
                    if (this.conItem.attachment_info[0].uid) {
                        this.temp_attachment_id_list = [];
                        for (let i = 0; i < this.conItem.attachment_info.length; i++) {
                            let file_item = {
                                temp_name: this.conItem.attachment_info[i].name,
                                temp_id: this.conItem.attachment_info[i].id,
                                temp_uid: this.conItem.attachment_info[i].uid
                            };
                            this.temp_attachment_id_list.push(file_item);
                        }
                    }
                    else {
                        setTimeout(this.attachmentInfoInit, 50);
                    }
                }

            },

            getProducts(ttype){
                let condition = {
                    type: ttype,
                    state: 1        //启用状态
                };
                let params = {
                    page_size: 100,
                    page_no: 1,
                    condition: condition
                };
                this.dynamicProducts = [];
                this.dynamicProducts2 = [];
                product_api.productList(params).then((res)=>{
                    let result = JSON.parse(res.data.datas);
                    if(res.data.status == 200){
                        var datas = result.items;

                        datas.forEach((item)=>{

                            // <!-- ============================================ 私募表单 end ================================================ -->
                            this.dynamicProducts.push( {
                                'key': item.product_name, 'value': item.id
                            });

                            // <!-- ============================================ 有限合伙等表单 start ================================================ -->
                            var name = item.product_name == null ? '' : item.product_name;
                            var p = item.investment_period == null ? '' : " - " + dict.contract_investment_period[item.investment_period];    //投资期限
                            var t = item.income_return_type == null ? '' : " - " + dict.contract_income_return_type[item.income_return_type];    //客户收益返还方式
                            var r = item.expected_earnings == null ? '' : " - " + (item.expected_earnings/10000) + '%'; //预期收益率
                            this.dynamicProducts2.push({
                                'key': (name + p + r + t),
                                'value': item.id,
                                'investment_period': item.investment_period,
                                'income_return_type': item.income_return_type,
                                'expected_earnings': item.expected_earnings,
                            });
                        });


                    }else{
                        if(res.data.status === 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.status+" "+res.data.msg);
                    }
                });
            },
            amountFormatter(val) {
                return commons.formatNum(val);
            },
            // 模糊查询
            wtCodeQuerySearchAsync(queryString, cb) {
                if (queryString) {
                    let condition = {
                        type: this.conItem.customer_type,
                        full_name: queryString
                    };
                    let params = {
                        page_size: 100,
                        page_no: 1,
                        condition: condition
                    };
                    customer_api.customerList(params).then((res) => {
                        let datas = JSON.parse(res.data.datas);
                        var result = [];
                        if(res.data.status == 200){
                            result = datas.items;
                            datas.items.forEach((item)=>{
                                var obj = {};
                                obj.uuid = 1;
                                obj.value = item.full_name + " (" + item.identity_no + ")";
                                obj.customer_full_name = item.full_name;
                                obj.customer_identity_type = item.identity_type + '';
                                obj.customer_identity_no = item.identity_no;
                                obj.customer_gender = item.gender + '';
                                obj.customer_mobile = item.mobile;
                                obj.customer_address = item.address;
                                obj.credit_no = item.credit_no;

                                var department_name = item.sale_department_name==null ? '' : '/' + item.sale_department_name;
                                var team_name = item.sale_team_name==null ? '' : '/' + item.sale_team_name;
                                var sale_position_name = item.sale_position_name==null ? '' : '/' + item.sale_position_name;
                                obj.sale_name_split = item.sale_name + department_name + team_name + sale_position_name;
                                obj.sale_id = item.sale_id;
                                result.push(obj);
                            })
                        }else{
                            result.push({
                                uuid: '-1',
                                value: '未搜索到结果！'
                            })
                        }
                        clearTimeout(this.timeout);
                        this.timeout = setTimeout(() => {
                            cb(result);
                        }, 100 * Math.random());
                    }).catch(_ => {});
                }
            },
            // 选择然后给其他dom元素赋值
            handleWtCodeSelect(item) {
                if (item.uuid == 1) {
                    // this.evId = item.uuid;
                    this.conItem.customer_full_name = item.customer_full_name;
                    this.conItem.customer_identity_type = item.customer_identity_type;
                    this.conItem.customer_identity_no = item.customer_identity_no;
                    this.conItem.customer_gender = item.customer_gender;
                    this.conItem.customer_mobile = item.customer_mobile;
                    this.conItem.customer_address = item.customer_address;
                    this.conItem.credit_no = item.credit_no;
                    this.conItem.sale_name_split = item.sale_name_split;
                    this.conItem.sale_id = item.sale_id;
                    //此处需要特别注意：当精确匹配查询时，当选中建议项时，需要把该对象的值赋给其他元素，
                    //页面数据不会马上更新，当再次点击输入框使输入框值有所变化时，页面数据才会更新出来,目前的解决办法为重新给输入值赋值时加上一个""
                    this.conItem.customer_full_name = item.customer_full_name + "";
                } else {
                    this.conItem.customer_full_name = "";
                }
            },
            // 模糊查询  销售人员
            querySearchAsyncSale(queryString, cb) {
                var wtParams = {};
                if (queryString) {
                    wtParams.full_name = queryString;
                    API.contractRetrieval(wtParams).then((res) => {
                        let datas = JSON.parse(res.data.datas);
                        var resultSale = [];
                        if(res.data.status == 200 && datas.length > 0){
                            datas.forEach((item)=>{
                                var obj = {};
                                var department_name = item.department_name==null ? '' : '/' + item.department_name;
                                var team_name = item.team_name==null ? '' : '/' + item.team_name;
                                var sale_position_name = item.sale_performance_state_name==null ? '' : '/' + item.sale_performance_state_name;
                                obj.uuid = 2;
                                obj.value = item.sale_name + department_name + team_name + sale_position_name;

                                obj.sale_name_split = item.sale_name + department_name + team_name + sale_position_name;
                                obj.sale_id = item.sale_id;
                                resultSale.push(obj);
                            })
                        }else{
                            resultSale.push({
                                uuid: '-1',
                                value: '未搜索到结果！'
                            })
                        }
                        clearTimeout(this.timeout);
                        this.timeout = setTimeout(() => {
                            cb(resultSale);
                        }, 100 * Math.random());
                    }).catch(_ => {});
                }
            },
            // 选择然后给其他dom元素赋值
            handleSelectSale(item) {
                if (item.uuid == 2) {
                    // this.evId = item.uuid;

                    this.conItem.sale_name_split = item.sale_name_split;

                    this.conItem.sale_id = item.sale_id;
                    //此处需要特别注意：当精确匹配查询时，当选中建议项时，需要把该对象的值赋给其他元素，
                    //页面数据不会马上更新，当再次点击输入框使输入框值有所变化时，页面数据才会更新出来,目前的解决办法为重新给输入值赋值时加上一个""
                    this.conItem.sale_name_split = item.sale_name_split + " ";
                    this.conItem.customer_full_name = this.conItem.customer_full_name + ' ';

                } else {
                    this.conItem.sale_name_split = "";
                }
            },

            /* ================================================ 编辑=========================================== */
            saveItem(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.importLoading = true;

                        this.getAttachIds();

                        this.showChangeVal(this.conItem);
                        let condition = Object.assign(this.conItem);
                        API.contractEdit(condition).then((res) => {
                            let result = JSON.parse(res.data.datas);
                            if (res.data.status === 200) {
                                this.$message({  type: 'success', message:" "+res.data.msg});
                                this.importLoading = false;
                                this.backButton();
                            }else{
                                this.$message.error(" "+res.data.msg);
                                this.importLoading = false;
                                this.dialogVisible = false;
                                this.showFailChangeVal(this.conItem);
                            }
                        });
                    }
                })
            },

            /* ======================================= 获取文件上传的id数组 =====================================*/
            getAttachIds() {
                this.conItem.attachment_id_list = [];
                for (let i = 0; i < this.temp_attachment_id_list.length; i++) {
                    this.conItem.attachment_id_list.push(this.temp_attachment_id_list[i].temp_id);
                }
                this.conItem.attachment_info = [];
            },


            /* ============================= 新增 =============================================================== */
            addSaveItem() {
                this.showChangeVal(this.conItem);
                let condition = Object.assign(this.conItem);
                API.contractAdd(condition).then((res) => {
                    let result = JSON.parse(res.data.datas);
                    if (res.data.status === 200) {
                        this.$message({  type: 'success', message:"合同录入成功，进入待审核队列！ "});
                        this.importLoading = false;
                        this.backButton();
                    }else{
                        this.$message.error(" "+res.data.msg);
                        this.importLoading = false;
                        this.dialogVisible = false;
                        this.showFailChangeVal(this.conItem);
                    }
                });
            },
            showChangeVal(tempConItem) {
                tempConItem.amount = tempConItem.amount * 100;
                if(tempConItem != 4){
                    tempConItem.expected_earnings = tempConItem.expected_earnings * 100;
                    tempConItem.month_return = tempConItem.month_return==''?'':tempConItem.month_return * 100;
                    tempConItem.expected_rate_of_return = tempConItem.expected_rate_of_return * 10000;
                }
            },
            showFailChangeVal(tempConItem) {
                tempConItem.amount = tempConItem.amount/100;
                if(tempConItem != 4){
                    tempConItem.expected_earnings = tempConItem.expected_earnings/100;
                    tempConItem.month_return = tempConItem.month_return==''?'':tempConItem.month_return/100;
                    tempConItem.expected_rate_of_return = tempConItem.expected_rate_of_return/10000;
                }
            },
            showItem(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.dialogVisible = true;
                        this.conDialogItem = this.conItem;
                    }
                });
            },
            backButton() {
                this.$router.push({path: '/contract/list'});
            },
            /* -------------------------------------- 上传文件 --------------------------------------------------  */
            // 上传前对文件的大小的判断
            beforeAvatarUpload (file) {
                const extension = file.name.split('.')[1] === 'pdf'
                const isLt2M = file.size / 1024 / 1024 < 2
                if (!extension) {
                    this.$message.error('上传模板只能是本地 pdf 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传模板大小不能超过 2MB!');
                }
                return extension && isLt2M;
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 20 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            handleRemove(file, fileList) {
                if (this.temp_attachment_id_list && this.temp_attachment_id_list.length > 0) {
                    let uid = file.uid;
                    for (let i = this.temp_attachment_id_list.length - 1; i >= 0; i--) {
                        if (this.temp_attachment_id_list[i].temp_uid == uid) {
                            this.temp_attachment_id_list.splice(i, 1);
                        }
                    }
                }

            },
            httpRequest(item){
                let formData = new FormData();
                formData.append('file', item.file);
                formData.append('roleType', "1");
                formData.append('fileName', item.file.name);
                let user = JSON.parse(sessionStorage.getItem("user"));
                formData.append('roleId', user.id);
                this.upload_file_names.push({'temp_file_name': item.file.name});

                update_file(formData).then((res) => {
                    let result = JSON.parse(res.data.datas);
                    if(res.data.status === 200) {

                        this.conItem.attachment_id_list.push(result.id);

                        /*// ================================ 此操作是多个文件上传，方便删除相应的上传文件返回的id =======================================*/
                        this.temp_attachment_id_list.push({
                            'temp_uid': item.file.uid,
                            'temp_name': item.file.name,
                            'temp_id': result.id
                        });
                    }else{
                        if(res.data.status === 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.msg);
                    }
                });
            }
        },
        watch: {
            // 监测路由变化,只要变化了就调用获取路由参数方法将数据存储本组件即可
            '$router': 'fetchData',
        },
        mounted(){
            this.$nextTick(function () {
                this.getProducts(this.conItem.type);
                var val = this.conItem.id;
                if(!(val == "" || val == undefined || val == null)){
                    this.conItem.id = this.$route.params.id;
                    this.getItem();         //获取详情
                }
            })
        }
    }
</script>
<style lang="scss" scoped>
    .el-col .el-col-24{
        text-align:left;font-weight: bolder;font-size: 14px;
    }
    .div-padd{
       // padding: 10px 230px;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 40%;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

</style>
